package com.bdd.controller;

import com.bdd.Result.Result;
import com.bdd.entity.Return;
import com.bdd.model.bo.OrderBO;
import com.bdd.model.bo.PageBO;
import com.bdd.model.bo.ReturnBO;
import com.bdd.model.bo.ReturnItemBO;
import com.bdd.model.bo.clothing.ClothingForOrderItemBO;
import com.bdd.service.OrderItemService;
import com.bdd.service.OrderService;
import com.bdd.service.ReturnItemService;
import com.bdd.service.ReturnService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

@RestController
@RequestMapping("/return")
@Api(value = "退货管理")
public class ReturnController {
    @Autowired
    ReturnService returnService;

    @GetMapping("/list")
    @ResponseBody
    @ApiOperation("退货列表")
//    @RequiresRoles(value = {"STAFF", "ADMIN"}, logical = Logical.OR)
    public Result<PageInfo<ReturnBO>> list(PageBO pageBO, Integer returnId){
        if(pageBO.getPageNum()<=0){
            pageBO.setPageNum(1);
        }
        if(pageBO.getPageSize()<=0){
            pageBO.setPageSize(10);
        }
        PageInfo<ReturnBO> returnBOList = new PageInfo<>(returnService.list(returnId,pageBO.getPageNum(),pageBO.getPageSize()));
        return Result.ok(returnBOList);
    }

    @GetMapping("/cacheList")
    @ResponseBody
    @ApiOperation("退货添加列表")
    @RequiresRoles(value = {"STAFF", "ADMIN"}, logical = Logical.OR)
    public Result<Map<String, Object>> cacheList() {
        Map<String, Object> map = returnService.cacheList();
        return Result.ok(map);
    }

    @PostMapping("/returnItem")
    @ResponseBody
    @ApiOperation("添加退货项返回列表")
    @RequiresRoles(value = {"STAFF", "ADMIN"}, logical = Logical.OR)
    public Result<Map<String, Object>> returnItem(@RequestParam("orderItemId") Integer orderItemId, @RequestParam("returnReason") String returnReason) {
        if (Objects.isNull(orderItemId)) {
            throw new IllegalArgumentException("订单项id不可为空");
        }
        returnService.returnItem(orderItemId, returnReason);
        Map<String, Object> map = returnService.cacheList();
        return Result.ok(map);
    }

    @PostMapping("/refund")
    @ResponseBody
    @ApiOperation("退款")
    public Result<Return> refund(Integer userId) {
        Return returnReturn = returnService.refund(userId);
        return Result.ok(returnReturn);
    }

    @PostMapping("/cancel")
    @ResponseBody
    @ApiOperation("取消退款")
    @RequiresRoles(value = {"STAFF", "ADMIN"}, logical = Logical.OR)
    public Result cancel(Integer userId) {
        returnService.cancel(userId);
        return Result.ok();
    }

}
